<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Detail;
use App\Admin\Repositories\Article;
use App\Enums\ArticleType;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class ArticleController extends AdminController
{
    public string $create_permission_name = 'article-create';
    public string $edit_permission_name = 'article-edit';
    public string $delete_permission_name = 'article-delete';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Article(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id');
            $grid->column('type')->display(function ($value) {
                return ArticleType::getDescription($value);
            });
            $grid->column('title');
            if ($this->edit && Admin::user()->can($this->edit_permission_name) && !$this->isTrashed()) {
                $grid->column('show')->switch();
            } else {
                $grid->column('show')->bool();
            }
            $grid->column('sort');
            $grid->column('created_at');
            $grid->column('updated_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title');
                $filter->equal('type')->select(ArticleType::asSelectArray());
                $filter->equal('show')->select(getBoolOption());
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append(new Detail());
            });
            $this->gridCommon($grid);
            if (!$this->edit || ($this->edit && Admin::user()->cannot($this->edit_permission_name)) || $this->isTrashed()) {
                $grid->disableEditButton();
            }
            $grid->disableQuickEditButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public function detail(mixed $id): Show
    {
        return Show::make($id, new Article(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('title');
            $show->field('show')->bool();
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(): Form
    {
        return Form::make(new Article(), function (Form $form) {
            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableCreatingCheck();
            $form->disableEditingCheck();
            $form->disableViewCheck();

            $form->select('type')->options(ArticleType::asSelectArray())->required();
            $form->text('title')->required();
            $form->editor('content')->required();
            $form->switch('show')->default(true);
            $form->number('sort')->default(100)->min(1)->max(100);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
